package com.houger.ddd.seckill.infra.datamanager;

import com.houger.ddd.common.utils.BusinessException;
import com.houger.ddd.common.utils.StatusCode;
import com.houger.ddd.datamanager.manager.aop.DataCache;
import com.houger.ddd.datamanager.manager.aop.DataCacheEvict;
import com.houger.ddd.datamanager.utils.CacheType;
import com.houger.ddd.seckill.infra.entity.ActivityInstance;
import com.houger.ddd.seckill.repository.ActivityInstanceRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InstanceDataManager {

    @Resource
    ActivityInstanceRepository instanceRepository;

    @DataCache(group = "instance",key = "('templateId=' + #templateId) + (':userId=' + #userId)",
        type = {CacheType.LOCAL, CacheType.REDIS},localExpire = 60, redisExpire = 1 * 60)
    public List<ActivityInstance> queryInstanceByTemplateIdAndUserId(Long templateId, String userId) {
        return this.instanceRepository.findByTemplateIdAndInitiatorId(templateId,userId).orElseThrow(()->new BusinessException(StatusCode.NOT_FOUND_IN_DB,null));
    }

    @DataCache(group = "instance",key = "('id=').concat(#id)",type = {CacheType.LOCAL, CacheType.REDIS},localExpire = 60, redisExpire = 1 * 60)
    public ActivityInstance queryById(Long id) {
        return this.queryByIdFromDb(id);
    }

    public ActivityInstance queryByIdFromDb(Long id) {
        return this.instanceRepository.findById(id).orElseThrow();
    }

    @DataCacheEvict(group = "instance",key = "('templateId=' + #templateId) + (':userId=' + #userId)",type = {CacheType.LOCAL, CacheType.REDIS})
    public void evict(Long templateId, Long userId){

    }

    @DataCacheEvict(group = "instance",key = "('id=').concat(#id)",type = {CacheType.LOCAL, CacheType.REDIS})
    public void evictById(Long id){

    }

}
